package cn.gson.springboot.model.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.MedicalCardDAO;
import cn.gson.springboot.model.dao.MedicalcarLossDAO;
import cn.gson.springboot.model.dao.ReplaceTabDAO;
import cn.gson.springboot.model.mapper.MedicalMapper;
import cn.gson.springboot.model.mapper.MlossMapper;
import cn.gson.springboot.model.mapper.ReplaceMapper;
import cn.gson.springboot.model.pojos.MedicalCardPx;
import cn.gson.springboot.model.pojos.MedicalcarLoss;
import cn.gson.springboot.model.pojos.ReplaceTab;
import cn.gson.springboot.model.pojos.User;

//医疗卡的serivce
@Service
@Transactional(rollbackFor=Exception.class)
public class MedicalService{
	@Autowired
	private  MedicalCardDAO mdao;
	
	@Autowired
	private MedicalMapper mp;
	
	//注解医疗卡挂失的dao方法
	@Autowired
	private MedicalcarLossDAO mldao;
	
	//注解医疗卡记录mapper
	@Autowired
	private MlossMapper  mlossmp;
	
	//注解补办记录DAO
	@Autowired
	private ReplaceTabDAO  repdao;
	
	//注解补办记录mapper
	@Autowired
	private ReplaceMapper  repmp;
	
	//医疗卡挂失记录mapper
	@Autowired
	private MlossMapper mloosm;
	
	//医疗卡新增
	public String  addmedical(MedicalCardPx m,String userName) {
		if(m!=null){
			//时间
			Date date = new Date();		
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String hhh=sdf.format(date);
			String hhhh = hhh.toString().replace("-", "");
			m.setMecardNumber(hhhh);
			m.setCardDate(new Date());
			m.setMedicalcardState(0);
			m.setBkje(new BigDecimal(10));
			m.setYlkblr(userName);
			mdao.save(m);
			return "1";
		}else {
			return "0";
		}
			
			
			
			
		
	}
	//医疗卡补办
	public String  buban(Integer mzcardId,String userName) {
			//根据医疗卡主键查询实体对象
			MedicalCardPx idc =mdao.findById(mzcardId).get();
			if(idc!=null) {
			 	//通过实体点出原来的医疗卡号方便新增补办记录
			 	String mid=idc.getMecardNumber();
			 	Date date = new Date();		
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				String hhh=sdf.format(date);
				String hhhh = hhh.toString().replace("-", "");
				
				//修改医疗卡卡号
				idc.setMecardNumber(hhhh);
				idc.setMedicalcardState(0);
				mdao.save(idc);
				
				//新增补办记录
				ReplaceTab r=new ReplaceTab();
				r.setMumber(mid);
				r.setReplaceMoney(new BigDecimal(10));
				r.setReplacedate(new Date());
				r.setMedicalCardPx(idc);
				
				r.setReplaceName(userName);
				repdao.save(r);
				return hhhh;
			}else {
				return "0";
			}	
		
	}
	//医疗卡挂失
	public String guaShi(Integer mzcardId,String userName){
		//根据医疗卡主键查询实体对象
		 MedicalCardPx idc = mdao.findById(mzcardId).get();
		 if(idc!=null){ 
			 //把医疗卡的修改为挂失的状态
			 idc.setMedicalcardState(1);
			 mdao.save(idc);
			 MedicalcarLoss mloss=new MedicalcarLoss();
			 
			 
			 //新增挂失记录
			 mloss.setGsblr(userName);
			 mloss.setLossDate(new Date());
			 mloss.setMedicalCardPx(idc);
			 mldao.save(mloss);
			 return "1";
		 }
		 	return "0";			 
	}
	
	//查询所有医疗卡
	public JSONArray finall(){
		return mp.findAll(); 
	}
	
	//<!-- 查询正常使用的医疗卡 -->
	public JSONArray canfindAll(){
		return mp.canfindAll(); 
	}
	
	//根据医疗卡卡号查整条对象
	public MedicalCardPx kahaoc(String mecardNumber) {
		 return mdao.khchak(mecardNumber);
	}

	//查询所有挂失记录
	public JSONArray finloss(){
		return mlossmp.findall();
	}
	
	//查询所有补办记录
	public JSONArray findreplace(){
		return repmp.findreplace();
	}
	//根据病人医疗卡主键查询用户信息 -->
	public  JSONArray zjmedical(Integer mzcard_id) {
		return mp.zjmedical(mzcard_id);
	}
	
	 //根据病人门诊号查询用户挂号信息 -->
	public  JSONArray menzh(Integer outpatient_id) {
		
		return mp.menzh(outpatient_id);
	}
	
	
	//医疗卡号查询医疗卡信息
	public JSONArray findid(String mecard_number){
		if(mecard_number!=null) {
			if(mecard_number.trim().equals("")) {
				mecard_number=null;
			}
		}
		JSONArray findid = mp.findid(mecard_number);
		return findid;
	}
	
	 //<根据医疗卡卡号查询病人挂号信息> -->
	public  JSONArray  idcgh(String mecard_number) {
		if(mecard_number!=null) {
			if(mecard_number.trim().equals("")) {
				mecard_number=null;
			}
		}
			JSONArray idcgh = mp.idcgh(mecard_number);
			return idcgh;
	}
	//根据医疗卡号查询挂失记录
	public JSONArray cardghjl(String mecard_number) {
		if(mecard_number!=null) {
			if(mecard_number.trim().equals("")) {
				mecard_number=null;
			}
		}
		JSONArray cardghjl = mloosm.cardghjl(mecard_number);
		return cardghjl;
	}
	
	//<!-- 根据医疗卡编号查询补办记录 -->
	public JSONArray bbjl(String mecard_number) {
		if(mecard_number!=null) {
			if(mecard_number.trim().equals("")) {
				mecard_number=null;
			}
		}
		JSONArray idbbjl = repmp.bbjl(mecard_number);
		return idbbjl;
	}
	
	
	//根据医疗卡卡号查询用户信息未挂失的医疗卡
	public JSONArray idmedical(String mecard_number) {
		if(mecard_number!=null) {
			if(mecard_number.trim().equals("")) {
				mecard_number=null;
			}
		}
			JSONArray idmedical = mp.idmedical(mecard_number);
			return idmedical;
	}
	
	//<!--根据身份证查询正常使用的医疗卡  -->
	public JSONArray sfzcx(String card_cid) {
		if(card_cid!=null) {
			if(card_cid.trim().equals("")) {
				card_cid=null;
			}
		}
		JSONArray sfzcx = mp.sfzcx(card_cid);
		return sfzcx;
	}
	
	//<!--根据身份证查询医疗卡  -->
	public JSONArray sfzsy(String card_cid) {
		if(card_cid!=null) {
			if(card_cid.trim().equals("")) {
				card_cid=null;
			}
		}
		JSONArray sfzsy = mp.sfzsy(card_cid);
		return sfzsy;
	}
		
	
	
}
